﻿package vklib 
{
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	/**
	 * ...
	 * @author apisoc@gmail.com
	 */
	public class VkRequestResult extends ResultBase
	{
		private var _req:URLRequest = new URLRequest();
		
		public function VkRequestResult() { }
		
		public function get request():URLRequest { return _req; }
		
		public override function load():void
		{
			try {
				var loader:URLLoader = new URLLoader();
				init(loader);
				loader.load(_req);
			} catch (e:Error) {
				VkApi.debug(e, this);
			}
		}
		protected override function successHandler(e:Event):void
		{
			e.target.removeEventListener(e.type, successHandler);
			if (handlers.onSuccess == null && handlers.onError == null) return;
			
			var data:Object = XML(e.target.data);
			if (data.localName() != 'error')
				handlers.onSuccess(data);
			else if (handlers.onError != null)
				handlers.onError(data.error_msg.toString() + '\n' + data.request_params.toString());
			else
				VkApi.debug(data.toString());
		}
	}

}